package com.sskj.b70.v.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.b70.R;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.adapter.BaseAdapter;
import com.sskj.b70.common.adapter.ViewHolder;
import com.sskj.b70.common.decoration.DividerLineItemDecoration;
import com.sskj.b70.common.widget.ToolBarLayout;
import com.sskj.b70.m.bean.AseetListBean;
import com.sskj.b70.p.activity.AssetManagePresenter;
import com.sskj.b70.v.base.BaseActivity;

import java.util.List;

import butterknife.BindView;

/**
 * ProjectName：bfex
 * DESC: (资产管理)
 * Created by 李岩 on 2018/6/29 0029
 * updateName:(修改人名称)
 * updateTime:(修改时间)
 * updateDesc:(修改内容)
 */
public class AssetManageActivity extends BaseActivity<AssetManagePresenter> implements View.OnClickListener, OnRefreshListener {
    @BindView(R.id.asset_manage_toolbar)
    ToolBarLayout mAssetManageToolbar;
    @BindView(R.id.asset_manage_recyclerview)
    RecyclerView mAssetManageRecyclerview;
    @BindView(R.id.asset_manage_smartrefresh)
    SmartRefreshLayout mAssetManageSmartrefresh;
    private BaseAdapter<AseetListBean.AseetBean> mAdapter;
    private TextView mAssetBalance;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_asset_manage;
    }

    @Override
    public AssetManagePresenter getPresenter() {
        return new AssetManagePresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        mAssetManageToolbar.setLeftButtonOnClickLinster(this);
        mAssetManageSmartrefresh.setRefreshHeader(new MaterialHeader(this));
        mAssetManageSmartrefresh.setOnRefreshListener(this);

        mAssetManageRecyclerview.setLayoutManager(new LinearLayoutManager(this));
//        mAssetManageRecyclerview.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        mAssetManageRecyclerview.addItemDecoration(new DividerLineItemDecoration(this)
                .setLastDraw(true)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 2))
                .setDividerColor(Color.parseColor("#f5f5f5")));
        mAdapter = new BaseAdapter<AseetListBean.AseetBean>(R.layout.item_asset_manage, null, mAssetManageRecyclerview) {

            @Override
            public void bind(ViewHolder holder, AseetListBean.AseetBean item) {
                holder.setText(R.id.item_asset_manage_type, item.pname);  // 类型--USDT
                holder.setText(R.id.item_asset_manage_available_balance, item.getUsableFund());// 可用金额
                holder.setText(R.id.item_asset_manage_frozen_capital, item.getFrostFund());  // 冻结金额
                holder.setOnClickListener(R.id.item_asset_manage_recharge, v -> {//充值
                    startActivity(new Intent(mActivity, RechargeActivity.class).putExtra(Constants.INTENT_ASSET_ID, item.id).putExtra(Constants.INTENT_ASSET_NAME, item.pname));
                });
                holder.setOnClickListener(R.id.item_asset_manage_withdraw, v -> {//提币
                    if (checkIdent()) {
                        startActivity(new Intent(mActivity, WithdrawActivity.class).putExtra(Constants.INTENT_ASSET_ID, item.id).putExtra(Constants.INTENT_ASSET_NAME, item.pname).putExtra("usableFund", item.getUsableFund()));
                    }
                });
                holder.setOnClickListener(R.id.item_asset_manage_record, v -> {//账户明细
                    startActivity(new Intent(mActivity, CapitalDetailsActivity.class).putExtra(Constants.INTENT_ASSET_ID, item.id).putExtra("stockCode", item.pname));
                });
                if (item.pname.equals("USDT") || item.pname.equals("BTC") || item.pname.equals("ETH") || item.pname.equals("PSTR") || item.pname.equals("BCH")) {
                    holder.setVisible(R.id.item_asset_manage_recharge, true);
                    holder.setVisible(R.id.item_asset_manage_withdraw, true);
                } else {
                    holder.setVisible(R.id.item_asset_manage_recharge, false);
                    holder.setVisible(R.id.item_asset_manage_withdraw, false);
                }
            }
        };
        View headerView = LayoutInflater.from(this).inflate(R.layout.layout_asset_header, null);
        mAssetBalance = headerView.findViewById(R.id.asset_balance);
        mAdapter.addHeaderView(headerView);
        mPresenter.requestUserAssetList();
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        mAssetManageSmartrefresh.finishRefresh();
        mPresenter.requestUserAssetList();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_leftbutton:
                finish();
                break;
        }
    }

    public void onDataSuccess(List<AseetListBean.AseetBean> data) {
//        mAssetBalance.setText(data.wallone);
        mAdapter.setNewData(data);
    }
}
